"""
Case Type   : create table as
Case Name   : 源表为外表，执行create table as
Create At   : 2024/4/16
Owner       : @li-xin12345
Description :
    1、创建postgres_fdw扩展，创建外部服务器、用户映射、模式
    2、建表作为远端表使用
    3、创建外表
    4、create table as 创建表
    5、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
    5、成功
History     :
"""

import os
import unittest

from yat.test import Node

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CreateTableAsTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.pri_user = Node('PrimaryDbUser')
        self.constant = Constant()
        self.server_name = 'server_cta_0042'
        self.schema_name = 'schema_cta_0042'
        self.tb_name = ['t_cta_0042_01', 't_cta_0042_02', 't_cta_0042_03']

    def test_create_table_as(self):
        text = '-----step1:创建postgres_fdw扩展，创建外部服务器、用户映射、模式;' \
               'expect:成功-----'
        self.log.info(text)
        sql = 'drop extension if exists postgres_fdw cascade;' \
              'create extension if not exists postgres_fdw;'
        res = self.pri_sh.execut_db_sql(sql)
        self.log.info(res)
        self.assertIn(self.constant.drop_extension_success, res,
                      '执行失败：' + text)
        self.assertIn(self.constant.create_extension_success, res,
                      '执行失败：' + text)
        sql = f"""drop server if exists {self.server_name} cascade;
        create server {self.server_name} foreign data wrapper postgres_fdw
        options (dbname '{self.pri_user.db_name}', port '{self.pri_user.db_port}');
        """
        res = self.pri_sh.execut_db_sql(sql)
        self.log.info(res)
        self.assertIn(self.constant.drop_server_success, res,
                      '执行失败：' + text)
        self.assertIn(self.constant.create_server_success, res,
                      '执行失败：' + text)

        sql = f'drop user mapping if exists for current_user ' \
              f'server {self.server_name};' \
              f'create user mapping for current_user server {self.server_name};'
        res = self.pri_sh.execut_db_sql(sql)
        self.log.info(res)
        self.assertIn(self.constant.drop_usermapping_success, res,
                      '执行失败：' + text)
        self.assertIn(self.constant.create_usermapping_success, res,
                      '执行失败：' + text)

        sql = f'drop schema if exists {self.schema_name} cascade;' \
              f'create schema {self.schema_name};'
        res = self.pri_sh.execut_db_sql(sql)
        self.log.info(res)
        self.assertIn(self.constant.DROP_SCHEMA_SUCCESS_MSG, res,
                      '执行失败：' + text)
        self.assertIn(self.constant.CREATE_SCHEMA_SUCCESS_MSG, res,
                      '执行失败：' + text)

        text = '-----step2:建表作为远端表使用;expect:成功-----'
        self.log.info(text)
        sql = f'drop table if exists {self.schema_name}.{self.tb_name[0]}; ' \
              f'create table {self.schema_name}.{self.tb_name[0]}' \
              f'(col_1 int, col_2 date, col_3 char(5));'
        res = self.pri_sh.execut_db_sql(sql)
        self.log.info(res)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, res,
                      '执行失败：' + text)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, res,
                      '执行失败：' + text)

        text = '-----step3:创建外表;expect:成功-----'
        self.log.info(text)
        sql = f"drop foreign table if exists {self.tb_name[1]};" \
              f"create foreign table {self.tb_name[1]}(col_1 int, " \
              f"col_2 date, col_3 char(5)) server {self.server_name} options " \
              f"(schema_name '{self.schema_name}', table_name '{self.tb_name[0]}');"
        res = self.pri_sh.execut_db_sql(sql)
        self.log.info(res)
        self.assertIn(self.constant.DROP_FOREIGN_SUCCESS_MSG, res,
                      '执行失败：' + text)
        self.assertIn(self.constant.CREATE_FOREIGN_SUCCESS_MSG, res,
                      '执行失败：' + text)

        text = '-----step4:创建表;expect:成功-----'
        self.log.info(text)
        sql = f'drop table if exists {self.schema_name}.{self.tb_name[2]};' \
              f'create table {self.schema_name}.{self.tb_name[2]} ' \
              f'as select * from {self.tb_name[1]};'
        res = self.pri_sh.execut_db_sql(sql)
        self.log.info(res)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, res,
                      '执行失败：' + text)
        self.assertIn('INSERT 0 0', res, '执行失败：' + text)

    def tearDown(self):
        text = '-----step4:清理环境;expect:成功-----'
        self.log.info(text)
        sql = f'drop extension if exists postgres_fdw cascade;' \
              f'drop server if exists {self.server_name} cascade;' \
              f'drop user mapping if exists for current_user ' \
              f'server {self.server_name};' \
              f'drop schema if exists {self.schema_name} cascade;' \
              f'drop foreign table if exists {self.tb_name[1]};'
        res = self.pri_sh.execut_db_sql(sql)
        self.log.info(res)
        self.assertIn(self.constant.drop_extension_success, res,
                      '执行失败：' + text)
        self.assertIn(self.constant.drop_server_success, res,
                      '执行失败：' + text)
        self.assertIn(self.constant.drop_usermapping_success, res,
                      '执行失败：' + text)
        self.assertIn(self.constant.DROP_SCHEMA_SUCCESS_MSG, res,
                      '执行失败：' + text)
        self.assertIn(self.constant.DROP_FOREIGN_SUCCESS_MSG, res,
                      '执行失败：' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
